import { _decorator, Component, instantiate, math, Node, NodePool, Pool, Prefab, Sprite, SpriteFrame } from 'cc';
import { ModuleBase } from '../../../FrameWork3.8.6/FrameWork/Module/ModuleBase';
import { ResMgr } from '../../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { PropDt } from '../../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { GameManager } from '../Manager/GameManager';
import { DataMgr } from '../../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { PropDtMgr } from '../../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { Emitter } from '../../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { Prop } from '../Object/Prop';
const { ccclass, property } = _decorator;

@ccclass('PropLayer')
export class PropLayer extends ModuleBase {

    //当前道具组信息
    private _arrPropDt:PropDt[]=[];

    //道具对象池
    private _propNodePool:Pool<Node>=null;

    //存储所有生成出来的道具的容器方便统一管理
    private _arrProp:Prop[]=[];
    //最终存储道具数据的容器
    private _arrGetPropDt:PropDt[]=[];
    get arrGetPropDt(){
        return this._arrGetPropDt;
    }    
    //给道具编号
    private _num:number=0;

    onInit(...rest: any[]): void {
        //初始化道具信息
        this.initPropDt();
        //初始化对象池
        this.initNodePool();
        
        //注册生成道具的消息
        Emitter.instance.on("addProp",this.randomProp,this);
        Emitter.instance.on("removeProp",this.removeProp,this);
    }

    //初始化道具数据的方法
    initPropDt(){
        for(let propID of GameManager.instance.propID){
            let propDt=DataMgr.instance.getMgr<PropDtMgr>("PropDtMgr").getDataByID<PropDt>(propID);
            this._arrPropDt.push(propDt);
        }
    }
    //初始化对象池数组的方法
    initNodePool(){
        let prefab=ResMgr.instance.getRes<Prefab>(BundleConfig.Game.name,"Prop");
        this._propNodePool=new Pool<Node>(()=>{
            let node=instantiate(prefab);
            return node;
        },5,(node:Node)=>{
            node.destroy();
        })
        
    }

    //随机确定生成道具的方法（生成随机道具）
    randomProp(node:Node){
        let random=math.random();
        if(random<=0.1){
            //生成宝箱
            this.addProp(node,0);
        }
        if(random>0.1&&random<0.3){
            this.addProp(node,1);
        }
    }

    //真正生成道具的方法node是生成道具的节点位置
    addProp(node:Node,propDtIndex:number){
        //先获取世界坐标
        let worldPos=node.worldPosition;
        //获取当前数据
        let propDt=this._arrPropDt[propDtIndex];

        let propNode=this._propNodePool.alloc();
        let spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,propDt.iconName);
        propNode.getComponent(Sprite).spriteFrame=spriteFrame;
        propNode.parent=this.node;
        propNode.setWorldPosition(worldPos);

        //获取脚本存储
        let propTs=propNode.getComponent(Prop);
        let propName=propDt.iconName+"Prop"+this._num;
        this._num++;
        propTs.onInit(propDt,propName);
        this._arrProp.push(propTs);
    }

    //销毁道具的方法，node是销毁的是哪个道具
    removeProp(node:Node,isGet:boolean){
        if(isGet){
            let propDt=node.getComponent(Prop).propDt;
            console.log("捡到道具了")
            this._arrGetPropDt.push(propDt);
        }
        if(this._arrProp.length<=0||!this._arrProp){
            console.log(this._arrProp);
            return;
        }
        for(let i=0;i<this._arrProp.length;i++){
            if(this._arrProp[i].name==node.getComponent(Prop).name){
                this._arrProp.splice(i,1);
                node.parent=null;
                this._propNodePool.free(node);
            }
        }
    }

    onUpdate(dt: number, ...rest: any[]): void {
        for(let prop of this._arrProp){
            prop.onUpdate(dt);
        }
    }


}


